package com.yms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yms.mapper.AdminRoleMapper;
import com.yms.mapper.MenuMapper;
import com.yms.mapper.RoleMenuMapper;
import com.yms.pojo.*;
import com.yms.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2020-03-20
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    //声明用户角色表的mapper
    @Autowired
    private AdminRoleMapper adminRoleMapper;
    //声明角色菜单表的mapper
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    //声明菜单表的mapper
    @Autowired
    private MenuMapper menuMapper;
    //加载当前用户的菜单信息
    /**
     * @param pid 当前要查询的菜单的上级ID
     * @param aid 当前用户的Id
     * @return
     */
    @Override
    public List<TreeResult> selMenuInfoService(String pid, Integer aid) {
        //1.获取当前用户的角色ID集合
            //创建条件构造器
            QueryWrapper<AdminRole> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("aid",aid).select("rid");
            //查询
            List<Object> rids = adminRoleMapper.selectObjs(queryWrapper);
        //2.获取角色对应的菜单的ID集合
            //创建条件构造器
            QueryWrapper<RoleMenu> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.in("rid",rids).select("mid");
            //查询
            List<Object> mids = roleMenuMapper.selectObjs(queryWrapper2);
        //3.获取上级Id为pid的菜单信息
            //创建条件构造器
            QueryWrapper<Menu> queryWrapper3=new QueryWrapper<>();
            queryWrapper3.in("mid",mids).eq("pid",pid);
            //查询
            List<Menu> menus = menuMapper.selectList(queryWrapper3);
        //4.将List<Menu>转换为List<TreeResult>
            //创建List<TreeResult>集合
            List<TreeResult> results=new ArrayList<>();
            //遍历转换
            for(Menu menu:menus){
                //创建TreeResult对象
                TreeResult result=new TreeResult();
                result.setId(menu.getMid());
                result.setText(menu.getMname());
                result.setState("1".equals(menu.getIsparent())?"closed":"open");
                //创建map集合,存自定义数据
                Map<String ,Object> map=new HashMap<>();
                map.put("isparent",menu.getIsparent());
                map.put("url",menu.getUrl());
                //增加一个键值对，为编辑菜单回显菜单描述提供便利
                map.put("mdesc",menu.getMdesc());
                result.setAttributes(map);
                //将result对象存储到集合中
                results.add(result);
            }


        return results;
    }

    @Override
    public Result addMenu(Menu menu) {
        Result result = new Result();
        int i = menuMapper.insert(menu);
        result.setSuccess(i>0?true:false);
        result.setMessage(i>0?"增加成功":"增加失败");
        return result;
    }

    @Override
    public Result updaMenu(Menu menu) {
        int i = menuMapper.updateById(menu);
        Result result = new Result();
        result.setSuccess(i>0?true:false);
        result.setMessage(i>0?"修改成功":"修改失败");
        return result;
    }

    @Override
    public Result deleteMenuById(Integer mid) {
        int i = menuMapper.deleteById(mid);
        Result result = new Result();
       result.setSuccess(i>0?true:false);
       result.setMessage(i>0?"删除失败":"删除成功");
        return result;
    }

    @Override
    public List<TreeResult> menuInfoALL(String pid) {
        //查询
        List<Menu> menus = menuMapper.selectList(new QueryWrapper<Menu>().eq("pid",pid));
        //4.将List<Menu>转换为List<TreeResult>
        //创建List<TreeResult>集合
        List<TreeResult> results=new ArrayList<>();
        //遍历转换
        for(Menu menu:menus){
            //创建TreeResult对象
            TreeResult result=new TreeResult();
            result.setId(menu.getMid());
            result.setText(menu.getMname());
            result.setState("1".equals(menu.getIsparent())?"closed":"open");
            //创建map集合,存自定义数据
            Map<String ,Object> map=new HashMap<>();
            map.put("isparent",menu.getIsparent());
            map.put("url",menu.getUrl());
            //增加一个键值对，为编辑菜单回显菜单描述提供便利
            map.put("mdesc",menu.getMdesc());
            result.setAttributes(map);
            //将result对象存储到集合中
            results.add(result);
        }
        return results;    }
}
